<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展 
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------

namespace app\admin\model\wddj\operation;

use think\Model;


class City extends Model
{

    

    

    // 表名
    protected $name = 'wddj_city';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
            $city_id = $row->id;
            $commissionRatioCreateData = array(
                'city_id'=>$city_id,
                'platform_commission_ratio'=>10.00,
                'driver_commission_ratio'=>90.00,
            );
            (new \app\admin\model\wddj\operation\CommissionRatio())->allowField(true)->save($commissionRatioCreateData);
            $startPriceModel = new \app\admin\model\wddj\operation\StartingPrice();
            $startPriceList = [
                ['city_id'=>$city_id,'start_time'=>'21:00:00','end_time'=>'23:59:59','price'=>42.00,'kilometer'=>6.0,'weigh'=>0],
                ['city_id'=>$city_id,'start_time'=>'19:00:00','end_time'=>'20:59:59','price'=>34.00,'kilometer'=>6.0,'weigh'=>0],
                ['city_id'=>$city_id,'start_time'=>'06:00:00','end_time'=>'18:59:59','price'=>29.00,'kilometer'=>6.0,'weigh'=>0],
                ['city_id'=>$city_id,'start_time'=>'00:00:00','end_time'=>'05:59:59','price'=>54.00,'kilometer'=>6.0,'weigh'=>0],
            ];
            $startPriceModel->saveAll($startPriceList);
            (new \app\admin\model\wddj\operation\CostAllocation())->allowField(true)->save([
                'city_id'=>$city_id,
                'mileage_fee_start_time'=>'00:00',
                'mileage_fee_end_time'=>'23:59',
                'mileage_price'=>4.5,
                'long_distance_fee_start_time'=>'00:00',
                'long_distance_fee_end_time'=>'23:59',
                'capped_price'=>200.0,
                'long_distance_price'=>1.0,
                'exceeding_kilometers'=>30.0,
                'duration_fee_start_time'=>'00:00',
                'duration_fee_end_time'=>'23:59',
                'free_waiting_time'=>10,
                'maximum_charging_duration'=>180,
                'duration_price'=>1.0,
            ]);
        });
    }

    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }






}
